
import { createSlice } from "@reduxjs/toolkit";

import { accountListApi , delAccountApi} from '../../apis/userApi';
const usersSlice=createSlice({

    name:"users",

    initialState:{
        data:[],
        total:0,
        loading:false

    },
    reducers: {
            setLoading(state,action){
                state.loading=action.payload
            },
            setDataAndTotal(state, action) {
                state.data=action.payload.data
                state.total=action.payload.total

            }
    }


})
export const {setLoading,setDataAndTotal}=usersSlice.actions

export default usersSlice.reducer;


//异步操作

export const getData=(params={pageSize:10,pageNumber:1}) =>async(dispatch)=>{
    try { 
      
      dispatch(setLoading(true))
      let res= await accountListApi(params)
    console.log('res',res)

    dispatch(setDataAndTotal({
      data:res.data ,total:res.totalCount
    }))
    
      dispatch(setLoading(false))
    } catch (error) {

      console.log (error)
      dispatch(setLoading(false))
  
    }
  }

  //删除
 export const del=(id)=>async(dispatch)=>{

    await  delAccountApi(id)

    dispatch(getData())
 }
